export default {
	pagePath: 'pages/index/index',
	hasLogin: false,
	user: {
		uid: '',
		name: '',
		phone: '',
		email: '',
		gender: '',
		qq: '',
		photo: ''
	},
	timeoutID: null,
	intervalID: null,
	setPagePath(path) {
		this.pagePath = path
	},
	setHasLogin(bool) {
		this.hasLogin = bool
	},
	setUser(userInfo){
		this.user = userInfo;
	},
	showLoading(titleInfo, showMask) {
		uni.showLoading({
			title: titleInfo,
			mask: showMask
		});
	},
	hideLoading() {
		uni.hideLoading();
	},
	/*带title的showModel*/
	showModalWithTitle(titleInfo, contentInfo, isShowCancel) {
		uni.showModal({
			title: titleInfo,
			content: contentInfo,
			showCancel: isShowCancel,
		})
	},
	showModal(contentInfo, isShowCancel, successCallback) {
		uni.showModal({
			content: contentInfo,
			showCancel: isShowCancel,
			success: (confirm, cancel) => {
				if (successCallback) {
					successCallback(confirm, cancel)
				}
			}
		})
	},
	/* api详情：https://uniapp.dcloud.io/api/ui/prompt?id=showtoast */
	showToast(titleInfo, iconType, showMask, durationTime) {
		uni.showToast({
			title: titleInfo,
			icon: iconType,
			mask: showMask,
			duration: durationTime
		})
	},
	hideToast() {
		uni.hideToast();
	},

	// 封装uni网络请求
	request(url, data, method, successCallback, failCallback, completeCallback) {
		uni.request({
			url: url,
			data: data,
			method: method,
			success: (res) => {
				if (successCallback) {
					successCallback(res)
				}
			},
			fail: (res) => {
				if (failCallback) {
					failCallback(res)
				}
			},
			complete: (res) => {
				if (completeCallback) {
					completeCallback(res)
				}
			}
		})
	},

	// 根据时间戳显示几天前、几周前等内容的转换
	previousTime(fulldatetime) {
		if (typeof(fulldatetime) == "undefined") {
			return '';
		}
		
		// #ifdef APP-PLUS
		// app需要转换一下格式
		fulldatetime = fulldatetime.replace(/-/g,"/");
		// #endif
		
		// 转换为时间戳
		let dateTimeStamp = new Date(fulldatetime).getTime();
		
		let minute = 1000 * 60;
		let hour = minute * 60;
		let day = hour * 24;
		let week = day * 7;
		let month = day * 30;
		// let year = month * 12;
		let now = new Date().getTime();
		let diffValue = now - dateTimeStamp; //时间差
		
		if (isNaN(diffValue) || diffValue < 0) {
			return '';
		}
		let minC = diffValue / minute;
		let hourC = diffValue / hour;
		let dayC = diffValue / day;
		let weekC = diffValue / week;
		let monthC = diffValue / month;
		// let yearC = diffValue / year;
		if (monthC >= 1 && monthC <= 5) {
			return parseInt(monthC) + '个月前';
		} else if (weekC >= 1 && weekC <= 3) {
			return parseInt(weekC) + '周前';
		} else if (dayC >= 1 && dayC <= 6) {
			return parseInt(dayC) + '天前';
		} else if (hourC >= 1 && hourC <= 23) {
			return parseInt(hourC) + '小时前';
		} else if (minC >= 1 && minC <= 59) {
			return parseInt(minC) + '分钟前';
		} else if (diffValue <= minute) {
			return '刚刚';
		} else {
			// let datetime = new Date();
			// datetime.setTime(dateTimeStamp);
			// let Nyear = datetime.getFullYear();
			// let Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
			// let Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
			// let Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
			// let Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
			// return Nyear + "-" + Nmonth + "-" + Ndate + " " + Nhour + ":" + Nminute;
			return fulldatetime;
		}
	},
	// 将数字转换为k、w单位
	numberFormat(number) {
		if (typeof(number) == "undefined" || isNaN(number)) {
			return '';
		}
		number = parseInt(number);
		if (number >= 10000) {
			return (number / 10000).toFixed(1) + 'w';
		} else if (number >= 1000) {
			return (number / 1000).toFixed(1) + 'k';
		} else {
			return number + '';
		}
	},
	//图片的资源路径转换
	photoUrlFormat(photo){
		return "http://localhost:8081/photo/"+photo;
	},
	imageUrlFormat(lfid,image){
		return "http://localhost:8081/image/"+lfid+"/"+image;
	}
}
